package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.WorkDistributeAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.ProcessContent;
import com.isunland.managesystem.entity.WorkDistribute;
import com.isunland.managesystem.entity.WorkDistributeOriginal;
import com.isunland.managesystem.utils.MessageUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import com.isunland.managesystem.widget.CheckLineView;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/5/12 14:31
 */
public class WorkDistributeListFragment extends BaseListFragment {
  protected static final String EXTRA_CONTENT =
      WorkDistributeListActivity.class.getSimpleName() + "EXTRA_CONTENT";
  private final int REQUEST_DETAIL = 0;
  @BindView(R.id.clv_instationNotify) CheckLineView clvInstationNotify;
  @BindView(R.id.clv_SMSNotify) CheckLineView clvSMSNotify;
  @BindView(R.id.clv_notifyAllParticipant) CheckLineView clvNotifyAllParticipant;
  @BindView(R.id.btn_send) Button btnSend;
  private ProcessContent mProcessContent;
  private ArrayList<WorkDistribute> mList;

  public static WorkDistributeListFragment newInstance(ProcessContent processContent) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, processContent);
    WorkDistributeListFragment fragment = new WorkDistributeListFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom("任务分配");
    mProcessContent = (ProcessContent) getArguments().getSerializable(EXTRA_CONTENT);
    mList = new ArrayList<>();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_add, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_add:
        //添加
        Intent intent = new Intent(getActivity(), WorkDistributeDetailActivity.class);
        intent.putExtra(WorkDistributeDetailFrament.EXTRA_PROCESSCONTENT, mProcessContent);
        startActivityForResult(intent, REQUEST_DETAIL);
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_WORK_DISTRIBUTR_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("planSourceId", mProcessContent.getId());//被分配任务id
    params.put("planKindType", mProcessContent.getPlanKindTypeCode());//计划属性
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    WorkDistributeOriginal result = new Gson().fromJson(json, WorkDistributeOriginal.class);
    if (result.getResult() == Base.SUCCESS && result.getRows() != null) {
      //获取列表成功
      mList.clear();
      mList.addAll(result.getRows());
      WorkDistributeAdapter mAdapter = new WorkDistributeAdapter(getActivity(), mList);
      setListAdapter(mAdapter);
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    WorkDistribute content = mList.get(position - 1);
    Intent intent = new Intent(getActivity(), WorkDistributeDetailActivity.class);
    intent.putExtra(WorkDistributeDetailFrament.EXTRA_CONTENT, content);
    intent.putExtra(WorkDistributeDetailFrament.EXTRA_PROCESSCONTENT, mProcessContent);
    startActivityForResult(intent, REQUEST_DETAIL);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_DETAIL) {
      volleyPost();
    }
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    View header =
        getLayoutInflater(savedInstanceState).inflate(R.layout.header_notify, mListview, false);
    ButterKnife.bind(this, header);
    mListview.addHeaderView(header);
    clvInstationNotify.getCbCheck()
        .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked && clvNotifyAllParticipant.getCbCheck().isChecked()) {
              clvNotifyAllParticipant.getCbCheck().setChecked(false);
            }
          }
        });
    clvNotifyAllParticipant.getCbCheck()
        .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
          @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked && clvInstationNotify.getCbCheck().isChecked()) {
              clvInstationNotify.getCbCheck().setChecked(false);
            }
          }
        });
    btnSend.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        String content = getString(R.string.newWorkMessage, getString(R.string.allocation));
        if (clvInstationNotify.getCbCheck().isChecked()) {
          //发送通知
          String excManNames = MyStringUtil.joinStr(mList, "excManName", getString(R.string.comma));
          String excManJobNOs = MyStringUtil.joinStr(mList, "excManId", getString(R.string.comma));
          MessageUtil.sendReport(mActivity, excManJobNOs, excManNames, content);
        }
        if (clvNotifyAllParticipant.getCbCheck().isChecked()) {
          //通知所有人
          String excManNames = MyStringUtil.joinStr(mList, "excManName", getString(R.string.comma));
          String coopManNames =
              MyStringUtil.joinStr(mList, "coopManNames", getString(R.string.comma));
          String excManJobNOs = MyStringUtil.joinStr(mList, "excManId", getString(R.string.comma));
          String coopManIds = MyStringUtil.joinStr(mList, "coopManIds", getString(R.string.comma));
          MessageUtil.sendReport(mActivity,
              MyStringUtil.connect(excManJobNOs, getString(R.string.comma), coopManIds),
              MyStringUtil.connect(excManNames, getString(R.string.comma), coopManNames), content);
        }
        if (clvSMSNotify.getCbCheck().isChecked()) {
          //发送短信
          String excManJobNOs = MyStringUtil.joinStr(mList, "excManId", getString(R.string.comma));
          MessageUtil.sendSMS(mActivity, excManJobNOs, content);
        }
      }
    });
  }
}
